﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using myMapObj;

namespace FakeGUI
{
    public partial class Form_MainForm : Form
    {
        #region 字段
        //选项变量
        private Color mZoomBoxColor = Color.DeepPink;
        private double mZoomBoxWidth = 0.53;
        private Color mSelectBoxColor = Color.DarkGreen;
        private double mSelectBoxWidth = 0.53;
        private double mZoomRatioFixed = 2;//固定缩放系数
        private double mZoomRatioMouseWheel = 1.2;//滑轮缩放系数
        private double mSelectingTolerance = 3;//容限
        private myMapObj.moSimpleFillSymbol mSelectingBoxSymbol;
        private myMapObj.moSimpleFillSymbol mZoomBoxSymbol;
        private myMapObj.moSimpleFillSymbol mMovingPolygonSymbol;
        private myMapObj.moSimpleFillSymbol mEditingPolygonSymbol;
        private myMapObj.moSimpleMarkerSymbol mEditingVertexSymbol;//顶点符号
        private myMapObj.moSimpleLineSymbol mElasticSymbol;//橡皮筋符号

        //与地图操作有关的变量
        private moMapOptionStyleConstant MapOptionStyle = moMapOptionStyleConstant.Pan;
        private PointF mStartMouseLocation;//拉框时的起点
        private bool mIsInZoomIn = false;
        private bool mIsInPan = false;
        private bool mIsInSelect = false;
        private bool mIsInIdentify = false;
        private bool mIsMovingShapes = false;
        private List<myMapObj.moGeometry> mMovingGeometries = new List<myMapObj.moGeometry>();//正在移动的图形集合
        private myMapObj.moGeometry mEditingGeometry;//正在编辑的图形
        private List<myMapObj.moPoints> mSketchingShape;//正在描绘的图形（存储为多点）

        //地图路径
        string MapPath = Application.StartupPath + "\\DefaultMaps\\默认地图.segm";
        //当前正在编辑的图层
        moMapLayer EditingLayer = null;
        //当前视图模式
        bool IsInDataView = true;
        //悬浮在按钮上的提示框
        ToolTip Tip = new ToolTip();
        //地图操作模式

        #endregion

        public Form_MainForm()
        {
            InitializeComponent();
            moMap.MouseWheel += moMap_MouseWheel;
        }


        #region 菜单栏

        #region 文件菜单(未完成)

        private void ToolStrip_新建_Click(object sender, EventArgs e)
        {
            Form_NewSEGM sForm_NewSEGM = new Form_NewSEGM();
            if(sForm_NewSEGM.ShowDialog()==DialogResult.OK)
            {
                try
                {
                    throw new NotImplementedException("未开发完成");
                    //NewMap(sNewSEGM.NewMapPath)
                }
                catch(Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }
            sForm_NewSEGM.Dispose();
        }
        private void ToolStrip_打开_Click(object sender, EventArgs e)
        {
            OpenFileDialog sOpenFileDialog = new OpenFileDialog
            {
                InitialDirectory = Application.StartupPath + "\\DefaultMaps",
                Filter = "SESSGIS地图|*.segm",
                Title = "打开",
                RestoreDirectory = true
            };
            if (sOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                MapPath = sOpenFileDialog.FileName;
                try
                {
                    SEGM test = new SEGM(MapPath);
                    moMap.Layers.Add(test.MyMap.GetItem(0));
                    moMap.RedrawMap();
                    //throw new NotImplementedException("未开发完成");
                    //LoadSEGM(MapPath);
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                }

            }
            sOpenFileDialog.Dispose();
        }

        private void ToolStrip_添加数据_Click(object sender, EventArgs e)
        {
            OpenFileDialog sOpenFileDialog = new OpenFileDialog
            {
                InitialDirectory = Application.StartupPath,
                Filter = "SESSGIS图层|*.segv|Shapefile|*.shp",
                Title = "添加数据",
                RestoreDirectory = true
            };
            if(sOpenFileDialog.ShowDialog()==DialogResult.OK)
            {
                string DataPath = sOpenFileDialog.FileName;
                try
                {
                    
                    
                }
                catch(Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }
            sOpenFileDialog.Dispose();
        }
        private void ToolStrip_保存_Click(object sender, EventArgs e)
        {
            try
            {
                throw new NotImplementedException("未开发完成");
                //SaveSEGM(MapPath)
            }
            catch(Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }

        private void ToolStrip_另存为_Click(object sender, EventArgs e)
        {
            SaveFileDialog sSaveFileDialog = new SaveFileDialog
            {
                FileName = "无标题",
                Title = "另存为",
                Filter = "SESSGIS地图|*.segm",
                AddExtension = true,
                DefaultExt = "segm",
                RestoreDirectory = true
            };
            if (sSaveFileDialog.ShowDialog()==DialogResult.OK)
            {
                try
                {
                    File.Copy(MapPath, sSaveFileDialog.FileName, false);
                }
                catch(Exception error)
                {
                    MessageBox.Show(error.Message);
                }
                
            }
            sSaveFileDialog.Dispose();
        }

        private void ToolStrip_导出地图_Click(object sender, EventArgs e)
        {
            SaveFileDialog sSaveFileDialog = new SaveFileDialog
            {
                FileName = "无标题",
                Title = "导出地图",
                Filter = "png|*.png|jpg|*.jpg|bmp|*.bmp",
                AddExtension = true,
                DefaultExt = "jpg",
                RestoreDirectory = true
            };
            if(sSaveFileDialog.ShowDialog()==DialogResult.OK)
            {
                try
                {
                    throw new NotImplementedException("未开发完成");
                    //SaveMap(Path,Format)
                }
                catch(Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }
            sSaveFileDialog.Dispose();
        }

        private void ToolStrip_关闭_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        #endregion

        #region 编辑菜单(未完成)

        #endregion

        #region 视图菜单(已完成)
        private void ToolStrip_数据视图_Click(object sender, EventArgs e)
        {
            //直接触发SplitButton的事件即可
            SplitButton_数据视图_Click(this, e);
        }
        private void ToolStrip_布局视图_Click(object sender, EventArgs e)
        {
            //直接触发SplitButton的事件即可
            SplitButton_布局视图_Click(this, e);
        }

        #endregion

        #region 插入菜单(未完成)

        #endregion

        #region 选择菜单(未完成)
        private void ToolStrip_按属性选择_Click(object sender, EventArgs e)
        {
            Form_SelectByAttr sForm_SelectByAttr = new Form_SelectByAttr(moMap.Layers);
            sForm_SelectByAttr.Show();
        }
        private void ToolStrip_清除所选项_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < moMap.Layers.Count; ++i)
            {
                moMap.Layers.GetItem(i).ClearSelection();
            }
        }
        #endregion

        #region 帮助菜单(已完成)
        private void ToolStrip_关于_Click(object sender, EventArgs e)
        {
            Form_AboutSSSGIS sAboutSESSGIS = new Form_AboutSSSGIS();
            sAboutSESSGIS.Show();
        }

        #endregion

        #endregion

        #region 按钮栏

        #region 新建按钮
        private void Button_New_Click(object sender, EventArgs e)
        {
            ToolStrip_新建_Click(this, e);
        }
        private void Button_New_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("新建\n创建新的SEGM地图文档", (Button)sender);
            Button_New.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void Button_New_MouseLeave(object sender, EventArgs e)
        {
            Button_New.BackgroundImageLayout = ImageLayout.Center;
        }

        #endregion

        #region 打开按钮
        private void Button_Open_Click(object sender, EventArgs e)
        {
            ToolStrip_打开_Click(this, e);
        }
        private void Button_Open_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("打开\n打开现有的SEGM地图文档", (Button)sender);
            Button_Open.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void Button_Open_MouseLeave(object sender, EventArgs e)
        {
            Button_Open.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 保存按钮
        private void Button_Save_Click(object sender, EventArgs e)
        {
            ToolStrip_保存_Click(this, e);
        }
        private void Button_Save_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("保存\n保存当前的SEGM地图文档", (Button)sender);
            Button_Save.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void Button_Save_MouseLeave(object sender, EventArgs e)
        {
            Button_Save.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 添加数据按钮
        private void Button_AddData_Click(object sender, EventArgs e)
        {
            //ToolStrip_添加数据_Click(this, e);
            OpenFileDialog sDialog = new OpenFileDialog();
            string sFileName = "";
            if (sDialog.ShowDialog() == DialogResult.OK)
            {
                sFileName = sDialog.FileName;
                sDialog.Dispose();
            }
            else
            {
                sDialog.Dispose();
                return;
            }
            try
            {
                /*
                FileStream sStream = new FileStream(sFileName, FileMode.Open);
                BinaryReader sr = new BinaryReader(sStream);*/
                myMapObj.Shapefile shp = new Shapefile(sFileName);
                myMapObj.moMapLayer sLayer = shp.LoadMapLayer();
                moMap.Layers.Add(sLayer);
                if (moMap.Layers.Count == 1)
                {
                    moMap.FullExtent();
                }
                else
                {
                    moMap.RedrawMap();
                }
                /*sr.Dispose();
                sStream.Dispose();*/
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }
        private void Button_AddData_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("添加数据\n将新的SEGV图层文件或Shp文件添加到当前的地图数据框中", (Button)sender);
            Button_AddData.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void Button_AddData_MouseLeave(object sender, EventArgs e)
        {
            Button_AddData.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 放大按钮
        private void Button_ZoomIn_Click(object sender, EventArgs e)
        {
            MapOptionStyle = moMapOptionStyleConstant.ZoomIn;
            ChangeMapCursor(Properties.Resources.ZoomIn);
        }

        private void Button_ZoomIn_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("放大\n通过在地图上拖拽框或单击的方式放大", (Button)sender);
            Button_ZoomIn.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void Button_ZoomIn_MouseLeave(object sender, EventArgs e)
        {
            Button_ZoomIn.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 缩小按钮
        private void Button_ZoomOut_Click(object sender, EventArgs e)
        {
            MapOptionStyle = moMapOptionStyleConstant.ZoomOut;
            ChangeMapCursor(Properties.Resources.ZoomOut);
        }

        private void Button_ZoomOut_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("缩小\n通过在地图上拖拽框或单击的方式缩小", (Button)sender);
            Button_ZoomOut.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void Button_ZoomOut_MouseLeave(object sender, EventArgs e)
        {
            Button_ZoomOut.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 漫游按钮
        private void Button_Pan_Click(object sender, EventArgs e)
        {
            MapOptionStyle = moMapOptionStyleConstant.Pan;
            ChangeMapCursor(Properties.Resources.PanUp);
        }
        private void Button_Pan_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("漫游\n通过在地图上拖拽实现漫游", (Button)sender);
            Button_Pan.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void Button_Pan_MouseLeave(object sender, EventArgs e)
        {
            Button_Pan.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 全图显示按钮
        private void Button_FullExtent_Click(object sender, EventArgs e)
        {
            moMap.FullExtent();
        }
        private void Button_FullExtent_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("全图显示\n缩放至地图的全图范围", (Button)sender);
            Button_FullExtent.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void Button_FullExtent_MouseLeave(object sender, EventArgs e)
        {
            Button_FullExtent.BackgroundImageLayout = ImageLayout.Center;
        }


        #endregion

        #region 选择元素按钮
        private void Button_SelectElement_Click(object sender, EventArgs e)
        {
            MapOptionStyle = moMapOptionStyleConstant.SelectElement;
            moMap.Cursor = Cursors.Default;
        }

        private void Button_SelectElement_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("选择元素\n选择、移动和调整放置到地图上的文本、图形和其他元素", (Button)sender);
            Button_SelectElement.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void Button_SelectElement_MouseLeave(object sender, EventArgs e)
        {
            Button_SelectElement.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 查询按钮
        private void Button_Identify_Click(object sender, EventArgs e)
        {
            MapOptionStyle = moMapOptionStyleConstant.Identify;
            moMap.Cursor = Cursors.Default;
        }
        private void Button_Identify_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("查询\n通过单击或拖框来查询要素属性值", (Button)sender);
            Button_Identify.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void Button_Identify_MouseLeave(object sender, EventArgs e)
        {
            Button_Identify.BackgroundImageLayout = ImageLayout.Center;
        }


        #endregion

        #region 框选按钮
        private void Button_SelectByBox_Click(object sender, EventArgs e)
        {
            MapOptionStyle = moMapOptionStyleConstant.Select;
        }
        private void Button_SelectByBox_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("选择\n通过拖拽框或点击来选择要素", (Button)sender);
            Button_SelectByBox.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void Button_SelectByBox_MouseLeave(object sender, EventArgs e)
        {
            Button_SelectByBox.BackgroundImageLayout = ImageLayout.Center;
        }
        #endregion

        #region 清除所选项按钮
        private void Button_ClearSelect_Click(object sender, EventArgs e)
        {
            moMap.Layers.GetItem(0).SelectedFeatures.Clear();
            moMap.RedrawMap();
        }
        private void Button_ClearSelect_MouseHover(object sender, EventArgs e)
        {
            Tip.Show("清除所选项\n", (Button)sender);
            Button_ClearSelect.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void Button_ClearSelect_MouseLeave(object sender, EventArgs e)
        {
            Button_ClearSelect.BackgroundImageLayout = ImageLayout.Center;
            
        }
        #endregion

        #endregion

        #region 图层栏

        #endregion

        #region 状态条
        private void SplitButton_数据视图_Click(object sender, EventArgs e)
        {
            SplitButton.Image = SplitButton_数据视图.Image;
            throw new NotImplementedException("未开发完成");
        }
        private void SplitButton_布局视图_Click(object sender, EventArgs e)
        {
            SplitButton.Image = SplitButton_布局视图.Image;
            throw new NotImplementedException("未开发完成");
        }
        private void moMap_MapScaleChanged(object sender)
        {
            StatusLabel_Scale.Text = "1: " + moMap.MapScale.ToString("0.000");
            
        }















        #endregion

        private void ChangeMapCursor(Icon CursorIcon)
        {
            //throw new NotImplementedException("未开发完成");
        }

        private void InitializeSymbols()
        {
            mSelectingBoxSymbol = new myMapObj.moSimpleFillSymbol();
            mSelectingBoxSymbol.Color = Color.Transparent;
            mSelectingBoxSymbol.Outline.Color = mSelectBoxColor;
            mSelectingBoxSymbol.Outline.Size = mSelectBoxWidth;
            mZoomBoxSymbol = new myMapObj.moSimpleFillSymbol();
            mZoomBoxSymbol.Color = Color.Transparent;
            mZoomBoxSymbol.Outline.Color = mZoomBoxColor;
            mZoomBoxSymbol.Outline.Size = mZoomBoxWidth;
            mMovingPolygonSymbol = new myMapObj.moSimpleFillSymbol();
            mMovingPolygonSymbol.Color = Color.Transparent;
            mMovingPolygonSymbol.Outline.Color = Color.Black;
            mEditingPolygonSymbol = new myMapObj.moSimpleFillSymbol();
            mEditingPolygonSymbol.Color = Color.Transparent;
            mEditingPolygonSymbol.Outline.Color = Color.DarkGreen;
            mEditingPolygonSymbol.Outline.Size = 0.53;
            mEditingVertexSymbol = new myMapObj.moSimpleMarkerSymbol();
            mEditingVertexSymbol.Color = Color.DarkGreen;
            mEditingVertexSymbol.Style = myMapObj.moSimpleMarkerSymbolStyleConstant.SolidSquare;
            mEditingVertexSymbol.Size = 2;
            mElasticSymbol = new myMapObj.moSimpleLineSymbol();
            mElasticSymbol.Color = Color.DarkGreen;
            mElasticSymbol.Size = 0.52;
            mElasticSymbol.Style = myMapObj.moSimpleLineSymbolStyleConstant.Dash;
        }

        private void Form_MainForm_Load(object sender, EventArgs e)
        {
            InitializeSymbols();
        }

        private void moMap_MouseClick(object sender, MouseEventArgs e)
        {
            if(MapOptionStyle == moMapOptionStyleConstant.ZoomOut)
            {
                myMapObj.moPoint sPoint = moMap.ToMapPoint(e.Location.X, e.Location.Y);
                moMap.ZoomByCenter(sPoint, 1 / mZoomRatioFixed);
            }
            else if(MapOptionStyle == moMapOptionStyleConstant.Sketch)
            {
                //将屏幕坐标转化为地图坐标，并加入描绘图形
                myMapObj.moPoint sPoint = moMap.ToMapPoint(e.Location.X, e.Location.Y);
                mSketchingShape.Last().Add(sPoint);
                moMap.RedrawTrackingShapes();
            }
            else
            {
                return;
            }
        }
        private void moMap_MouseWheel(object sender, MouseEventArgs e)
        {
            //计算地图控件中心点的地图坐标
            double sX = moMap.ClientRectangle.Width / 2;
            double sY = moMap.ClientRectangle.Height / 2;
            myMapObj.moPoint sPoint = moMap.ToMapPoint(sX, sY);
            if (e.Delta > 0)
            {
                moMap.ZoomByCenter(sPoint, mZoomRatioMouseWheel);
            }
            else
            {
                moMap.ZoomByCenter(sPoint, 1 / mZoomRatioMouseWheel);
            }
        }
        private void moMap_MouseDown(object sender, MouseEventArgs e)
        {
            if(MapOptionStyle == moMapOptionStyleConstant.Pan)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mStartMouseLocation = e.Location;
                    mIsInPan = true;
                }
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.ZoomIn)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mStartMouseLocation = e.Location;
                    mIsInZoomIn = true;
                }
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.Select)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mStartMouseLocation = e.Location;
                    mIsInSelect = true;
                }
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.Identify)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mStartMouseLocation = e.Location;
                    mIsInIdentify = true;
                }
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.MoveFeature)
            {
                if (e.Button != MouseButtons.Left)
                {
                    return;
                }
                myMapObj.moMapLayer sLayer = new moMapLayer();//老师的程序只能move多边形，这里需要准确的寻找到移动的要素所在图层
                if (sLayer == null)
                {
                    return;
                }
                Int32 sSelFeatureCount = sLayer.SelectedFeatures.Count;
                if (sSelFeatureCount == 0)
                {
                    return;
                }
                mMovingGeometries.Clear();
                for (Int32 i = 0; i < sSelFeatureCount; ++i)
                {
                    myMapObj.moMultiPolygon sOriPolygon = (myMapObj.moMultiPolygon)sLayer.SelectedFeatures.GetItem(i).Geometry;
                    myMapObj.moMultiPolygon sDesPolygon = sOriPolygon.Clone();
                    mMovingGeometries.Add(sDesPolygon);
                }
                mStartMouseLocation = e.Location;
                mIsMovingShapes = true;
            }
            else
            {
                return;
            }
            
        }

        private void moMap_MouseMove(object sender, MouseEventArgs e)
        {
            if(MapOptionStyle == moMapOptionStyleConstant.Pan)
            {
                if (!mIsInPan)
                {
                    return;
                }
                moMap.PanMapImageTo(e.Location.X - mStartMouseLocation.X, e.Location.Y - mStartMouseLocation.Y);

            }
            else if (MapOptionStyle == moMapOptionStyleConstant.ZoomIn)
            {
                if (!mIsInZoomIn)
                {
                    return;
                }
                moMap.Refresh();
                myMapObj.moRectangle sRect = GetMapRectByTwoPoints(mStartMouseLocation, e.Location);
                myMapObj.moUserDrawingTool sDrawingTool = moMap.GetDrawingTool();
                sDrawingTool.DrawRectangle(sRect, mZoomBoxSymbol);
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.Select)
            {
                if (!mIsInSelect)
                {
                    return;
                }
                moMap.Refresh();
                myMapObj.moRectangle sRect = GetMapRectByTwoPoints(mStartMouseLocation, e.Location);
                myMapObj.moUserDrawingTool sDrawingTool = moMap.GetDrawingTool();
                sDrawingTool.DrawRectangle(sRect, mSelectingBoxSymbol);
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.Identify)
            {
                if (!mIsInIdentify)
                {
                    return;
                }
                moMap.Refresh();
                myMapObj.moRectangle sRect = GetMapRectByTwoPoints(mStartMouseLocation, e.Location);
                myMapObj.moUserDrawingTool sDrawingTool = moMap.GetDrawingTool();
                sDrawingTool.DrawRectangle(sRect, mSelectingBoxSymbol);
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.MoveFeature)
            {
                if (!mIsMovingShapes)
                {
                    return;
                }
                double sDeltaX = moMap.ToMapDistance(e.Location.X - mStartMouseLocation.X);
                double sDeltaY = moMap.ToMapDistance(mStartMouseLocation.Y - e.Location.Y);
                ModifyMovingGeometries(sDeltaX, sDeltaY);
                moMap.Refresh();
                DrawMovingShapes();
                mStartMouseLocation = e.Location;
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.Sketch)
            {
                //橡皮筋功能
                myMapObj.moPoint sCurPoint = moMap.ToMapPoint(e.Location.X, e.Location.Y);
                myMapObj.moPoints sLastPart = mSketchingShape.Last();
                Int32 sPointCount = sLastPart.Count;
                if (sPointCount == 0)
                {
                    //do nothing
                }
                else if (sPointCount == 1)
                {
                    moMap.Refresh();
                    myMapObj.moPoint sFirstPoint = sLastPart.GetItem(0);
                    myMapObj.moUserDrawingTool sDrawingTool = moMap.GetDrawingTool();
                    sDrawingTool.DrawLine(sFirstPoint, sCurPoint, mElasticSymbol);
                }
                else
                {
                    moMap.Refresh();
                    myMapObj.moPoint sFirstPoint = sLastPart.GetItem(0);
                    myMapObj.moPoint sLastPoint = sLastPart.GetItem(sPointCount - 1);
                    myMapObj.moUserDrawingTool sDrawingTool = moMap.GetDrawingTool();
                    sDrawingTool.DrawLine(sFirstPoint, sCurPoint, mElasticSymbol);
                    sDrawingTool.DrawLine(sLastPoint, sCurPoint, mElasticSymbol);
                }
            }
            else
            {
                return;
            }
        }

        private void moMap_MouseUp(object sender, MouseEventArgs e)
        {
            if(MapOptionStyle == moMapOptionStyleConstant.Pan)
            {
                if (!mIsInPan)
                {
                    return;
                }
                mIsInPan = false;
                double sDeltaX = moMap.ToMapDistance(e.Location.X - mStartMouseLocation.X);
                double sDeltaY = moMap.ToMapDistance(mStartMouseLocation.Y - e.Location.Y);
                moMap.PanDelta(sDeltaX, sDeltaY);
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.ZoomIn)
            {
                if (!mIsInZoomIn)
                {
                    return;
                }
                mIsInZoomIn = false;
                if (mStartMouseLocation.X == e.Location.X || mStartMouseLocation.Y == e.Location.Y)//单点放大
                {
                    myMapObj.moPoint sPoint = moMap.ToMapPoint(mStartMouseLocation.X, mStartMouseLocation.Y);
                    moMap.ZoomByCenter(sPoint, mZoomRatioFixed);
                }
                else//矩形放大
                {
                    myMapObj.moRectangle sBox = GetMapRectByTwoPoints(mStartMouseLocation, e.Location);
                    moMap.ZoomToExtent(sBox);
                }
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.Select)
            {
                if (!mIsInSelect)
                {
                    return;
                }
                mIsInSelect = false;
                myMapObj.moRectangle sBox = GetMapRectByTwoPoints(mStartMouseLocation, e.Location);
                double tolerance = moMap.ToMapDistance(mSelectingTolerance);
                moMap.SelectByBox(sBox, tolerance, 0);
                moMap.RedrawTrackingShapes();
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.Identify)
            {
                if (!mIsInIdentify)
                {
                    return;
                }
                mIsInIdentify = false;
                moMap.Refresh();
                myMapObj.moRectangle sBox = GetMapRectByTwoPoints(mStartMouseLocation, e.Location);
                double tolerance = moMap.ToMapDistance(mSelectingTolerance);
                if (moMap.Layers.Count == 0)
                {
                    return;
                }
                else
                {
                    myMapObj.moMapLayer sLayer = moMap.Layers.GetItem(0);
                    myMapObj.moFeatures sFeatures = sLayer.SearchByBox(sBox, tolerance);
                    //弹出Identify窗体
                    Int32 sSelFeaturesCount = sFeatures.Count;
                    if (sSelFeaturesCount > 0)
                    {
                        myMapObj.moGeometry[] sGeometries = new myMapObj.moGeometry[sSelFeaturesCount];
                        for (Int32 i = 0; i < sSelFeaturesCount; ++i)
                        {
                            sGeometries[i] = sFeatures.GetItem(i).Geometry;
                        }
                        moMap.FlashShapes(sGeometries, 3, 800);
                    }


                }
            }
            else if (MapOptionStyle == moMapOptionStyleConstant.MoveFeature)
            {
                if (!mIsMovingShapes)
                {
                    return;
                }
                mIsMovingShapes = false;
                //在这需要修改相应的数据，否则一松开就回去了
                moMap.RedrawMap();
                mMovingGeometries.Clear();
            }
            else
            {
                return;
            }
        }

        private myMapObj.moRectangle GetMapRectByTwoPoints(PointF point1, PointF point2)
        {
            myMapObj.moPoint sPoint1 = moMap.ToMapPoint(point1.X, point1.Y);
            myMapObj.moPoint sPoint2 = moMap.ToMapPoint(point2.X, point2.Y);
            double sMinX = Math.Min(sPoint1.X, sPoint2.X);
            double sMaxX = Math.Max(sPoint1.X, sPoint2.X);
            double sMinY = Math.Min(sPoint1.Y, sPoint2.Y);
            double sMaxY = Math.Max(sPoint1.Y, sPoint2.Y);
            myMapObj.moRectangle sRect = new myMapObj.moRectangle(sMinX, sMaxX, sMinY, sMaxY);
            return sRect;
        }
        //获取一个多边形图层
        private myMapObj.moMapLayer GetPolygonLayer()
        {
            Int32 sLayerCount = moMap.Layers.Count;
            myMapObj.moMapLayer sLayer = null;
            for (Int32 i = 0; i <= sLayerCount - 1; i++)
            {
                if (moMap.Layers.GetItem(i).ShapeType == myMapObj.moGeometryTypeConstant.MultiPolygon)
                {
                    sLayer = moMap.Layers.GetItem(i);
                    break;
                }
            }
            return sLayer;
        }

        //修改移动图形的坐标
        private void ModifyMovingGeometries(double deltaX, double deltaY)
        {
            Int32 sCount = mMovingGeometries.Count;
            for (Int32 i = 0; i <= sCount - 1; i++)
            {
                if (mMovingGeometries[i].GetType() == typeof(myMapObj.moMultiPolygon))
                {
                    myMapObj.moMultiPolygon sMultiPolygon = (myMapObj.moMultiPolygon)mMovingGeometries[i];
                    Int32 sPartCount = sMultiPolygon.Parts.Count;
                    for (Int32 j = 0; j <= sPartCount - 1; j++)
                    {
                        myMapObj.moPoints sPoints = sMultiPolygon.Parts.GetItem(j);
                        Int32 sPointCount = sPoints.Count;
                        for (Int32 k = 0; k <= sPointCount - 1; k++)
                        {
                            myMapObj.moPoint sPoint = sPoints.GetItem(k);
                            sPoint.X = sPoint.X + deltaX;
                            sPoint.Y = sPoint.Y + deltaY;
                        }
                    }
                    sMultiPolygon.UpdateExtent();
                }
            }
        }
        //绘制移动图形
        private void DrawMovingShapes()
        {
            myMapObj.moUserDrawingTool sDrawingTool = moMap.GetDrawingTool();
            Int32 sCount = mMovingGeometries.Count;
            for (Int32 i = 0; i <= sCount - 1; i++)
            {
                if (mMovingGeometries[i].GetType() == typeof(myMapObj.moMultiPolygon))
                {
                    myMapObj.moMultiPolygon sMultiPolygon = (myMapObj.moMultiPolygon)mMovingGeometries[i];
                    sDrawingTool.DrawMultiPolygon(sMultiPolygon, mMovingPolygonSymbol);
                }
            }
        }
        //绘制正在描绘的图形
        private void DrawSketchingShapes(myMapObj.moUserDrawingTool drawingTool)
        {
            if (mSketchingShape == null)
                return;
            Int32 sPartCount = mSketchingShape.Count;
            //绘制已经描绘完成的部分
            for (Int32 i = 0; i <= sPartCount - 2; i++)
            {
                drawingTool.DrawPolygon(mSketchingShape[i], mEditingPolygonSymbol);
            }
            //正在描绘的部分（只有一个Part）
            myMapObj.moPoints sLastPart = mSketchingShape.Last();
            if (sLastPart.Count >= 2)
                drawingTool.DrawPolyline(sLastPart, mEditingPolygonSymbol.Outline);
            //绘制所有顶点手柄
            for (Int32 i = 0; i <= sPartCount - 1; i++)
            {
                myMapObj.moPoints sPoints = mSketchingShape[i];
                drawingTool.DrawPoints(sPoints, mEditingVertexSymbol);
            }
        }

        //绘制正在编辑的图形
        private void DrawEditingShapes(myMapObj.moUserDrawingTool drawingTool)
        {
            if (mEditingGeometry == null)
                return;
            if (mEditingGeometry.GetType() == typeof(myMapObj.moMultiPolygon))
            {
                myMapObj.moMultiPolygon sMultiPolygon = (myMapObj.moMultiPolygon)mEditingGeometry;
                //绘制边界
                drawingTool.DrawMultiPolygon(sMultiPolygon, mEditingPolygonSymbol);
                //绘制顶点手柄
                Int32 sPartCount = sMultiPolygon.Parts.Count;
                for (Int32 i = 0; i <= sPartCount - 1; i++)
                {
                    myMapObj.moPoints sPoints = sMultiPolygon.Parts.GetItem(i);
                    drawingTool.DrawPoints(sPoints, mEditingVertexSymbol);
                }
            }
        }

        private void InitializeSketchingShape()
        {
            mSketchingShape = new List<myMapObj.moPoints>();
            myMapObj.moPoints sPoints = new myMapObj.moPoints();
            mSketchingShape.Add(sPoints);
        }

        //根据屏幕坐标显示地图坐标
        private void ShowCoordinates(PointF point)
        {
            myMapObj.moPoint sPoint = moMap.ToMapPoint(point.X, point.Y);
            double sX = Math.Round(sPoint.X, 2);
            double sY = Math.Round(sPoint.Y, 2);
            StatusLabel_Coordinate.Text = "X:" + sX.ToString() + ", Y:" + sY.ToString();
        }

        private void 图层属性_Click(object sender, EventArgs e)
        {
            moMapLayer sLayer = moMap.Layers.GetItem(0);
            Form_LayerAttr sLayerAttr = new Form_LayerAttr(sLayer);
            if(sLayerAttr.ShowDialog()==DialogResult.OK)
            {
                sLayerAttr.SetLayerAttr(sLayer);
                moMap.RedrawMap();
            }
        }


    }
}
